/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.dao.impl;

import com.qdu.entity.PublicActivity;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.qdu.dao.PublicActivityDao;

/**
 *
 * @author Daniel
 */
public class PublicActivityDaoImpl extends BaseDaoImpl implements PublicActivityDao{

    @Override
    public int insert(PublicActivity p) {
        return executeUpdate("insert into Public_Activity values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",p.getPno(), p.getPtopic(),p.getPtype(),p.getPnum(),
                p.getPbstart(),p.getPbend(),p.getPbegin(),p.getPend(),p.getPmoney(),p.getPlocal(),p.getPintro(),p.getPps(),p.getPtag(),p.getPsit(),
                p.getUno() ); 
    }

    @Override
    public int update(PublicActivity p) {
        return executeUpdate("update Public_Activity set Plocal=?, Pps=?,Ptag=?, Psit=? where Pno=?",p.getPlocal(),p.getPps(),p.getPtag(), p.getPsit(),p.getPno()); 
    }

    @Override
    public int deleteByPno(int Pno) {
        return executeUpdate("delete Public_Activity where Pno=?", Pno);
    }

    @Override
    public List<PublicActivity> getAll() {
         Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<PublicActivity> list = new ArrayList<>();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Public_Activity ");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new PublicActivity(rs.getInt(1), rs.getString(2), rs.getString(3), 
                        rs.getInt(4),rs.getDate(5),rs.getDate(6),rs.getDate(7),
                        rs.getDate(8),rs.getInt(9),rs.getString(10),rs.getString(11),
                        rs.getString(12),rs.getString(13), rs.getString(14),rs.getInt(15)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

    @Override
    public PublicActivity getOneByPno(int Pno) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        PublicActivity pa = null;

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Public_Activity where Pno=?");
            ps.setInt(1, Pno);
            rs = ps.executeQuery();
            while (rs.next()) {
                pa = new PublicActivity(rs.getInt(1), rs.getString(2), rs.getString(3), 
                        rs.getInt(4),rs.getDate(5),rs.getDate(6),rs.getDate(7),
                        rs.getDate(8),rs.getInt(9),rs.getString(10),rs.getString(11),
                        rs.getString(12),rs.getString(13),rs.getString(14),rs.getInt(15));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return pa;
        
    }

    @Override
    public List<PublicActivity> getActivityByKeyword(String Ptopic) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<PublicActivity> list = new ArrayList<>();
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select Pno,Ptopic,Pnum,Pbstart,Pbend,Pbegin,Pend,Pintro from Public_Activity where Ptopic like ?");
            ps.setString(1, "%" + Ptopic + "%");
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new PublicActivity(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getDate(4),rs.getDate(5),rs.getDate(6),rs.getDate(7),
                        rs.getString(8)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

    @Override
    public int join(PublicActivity p) {
        return executeUpdate("update Public_Activity set Pnum=? where Pno=?",p.getPnum(),p.getPno()); 
    }

    @Override
    public List<PublicActivity> getActivityByUno(int Uno) {
         Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<PublicActivity> list = new ArrayList<>();
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Public_Activity where Uno=?");
            ps.setInt(1, Uno);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new PublicActivity(rs.getInt(1), rs.getString(2), rs.getString(3), 
                        rs.getInt(4),rs.getDate(5),rs.getDate(6),rs.getDate(7),
                        rs.getDate(8),rs.getInt(9),rs.getString(10),rs.getString(11),
                        rs.getString(12),rs.getString(13), rs.getString(14),rs.getInt(15)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

    
}
